const path = require("path");
const fs = require("fs");
const moment = require("moment");

class middlewares {
    static recordErrLogs(err, req, res, next) {
        let err_time = moment().format("YYYY-MM-DD HH:mm:ss");
        let log_path = path.join(process.cwd(), "err_logs", "err_logs.txt");
        let errMsgs = `
----------------------------------${err_time}------------------------------------------
---start---
    请求地址：${req.ip}
    请求方式：${req.method}
    请求路径：${req.url}
    错误名称：${err.name}
    错误信息：${err.message}
    错误堆栈：${err.stack}
---end----
        \n\n
        `;
        fs.appendFile(log_path, errMsgs, "utf8", (err) => {
            res.json({
                err_code: 1,
                message: "发生未知错误，请联系管理员！"
            });
            next();
        })
    }
};


module.exports = middlewares;
